<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');


# Depriciated use Tamer_Model shortcut tm tab



class Master_Model extends Model {

  
    public function setTable($master_table)
    {
   		$this->table = $master_table;
        return $this->table;
    }

    public function checkTable()
    {
        If( ! isset($this->table) ){ 
            redirect('dbview/pages/error_db', 'location'); 
        }else{
            return $this->table;
        }
    }

    public function initdb($id = FALSE)
    {
        if($id !== FALSE){
            $this->dbdata = $this->get_by_id($id);
        }else{
            $fields = $this->db->list_fields($this->table);
            foreach ($fields as $field) $this->dbdata[$field] = '';
        }
        return $this->dbdata;        
    }

    public function get_by_id($id, $table = FALSE) 
    {
        If( $table !== FALSE ){ $this->table = $table; }
        $this->checkTable();
        $return = array();
        $query = $this->db->query("SELECT * FROM `" .$this->table. "` WHERE id = '" .$id. "' LIMIT 1");
        
        if ($query->num_rows() > 0) {
            $return = $query->row_array();
        }else{
            $return = FALSE;
        }
        return $return;
    }
    
    
    // *************  Start Contact info ********************
    
    function get_uid() 
    {
        if( ! isset($this->uid) ) {
            $this->uid = $this->session->userdata('uid');
        }
        return $this->uid;	
    }
    
	function get_People_id() {
        if( ! isset($this->people_id)){
            $this->get_uid();
            $query = $this->db->query("SELECT `people_id` FROM `contact_user` WHERE uid = '$this->uid' LIMIT 1");
            if ($query->num_rows() > 0) {
                $row = $query->row();
                $this->people_id = $row->people_id;
            }else{
                redirect('dbview/pages/error_people_info', 'location');
            }
        }
		return $this->people_id;	
    }

    
	function get_Org_id() {
        if( ! isset($this->org_id ) ) {
            $this->get_uid();
            $query = $this->db->query("SELECT `org_id` FROM `contact_user` WHERE uid = '$this->uid' LIMIT 1");
            if ($query->num_rows() > 0) {
                $row = $query->row();
                $this->org_id = $row->org_id;
            }else{
                redirect('dbview/pages/error_company_info', 'location');
            }
        }
		return $this->org_id;	
    }

    
    // *************  Start Places info ********************
    
    
    function countrySelect($selected = False)
	{

        $sql = "SELECT FIPS, Country FROM places_country";
		$query = $this->db->query($sql);
		$output = '<select name="Country">';

		foreach ($query->result() as $row)
		{
            $sel = ($selected != $row->FIPS) ? '' : ' selected="selected"';
            $output .= '<option value="' . $row->FIPS. '" $sel>' .$row->Country. '</option>';
		}
		$output .= '</select>'; 
		return $output;	
	}

    function postcodeSelect($selected = '')
	{
        $query = $this->Placedb->Postcode($selected);
        
		$output = '<select name="postcode">';
		foreach ($query->result() as $row) {
            $sel = ($selected != $row->code) ? '' : ' selected="selected"';
            $output .= '<option value="'.$row->code. '" $sel. >' . $row->code. '</option>';
		}
		$output .= '</select>'; 
		return $output;		
	}

	function Country($searchstring = False)
	{
        if($searchstring === False)
        {
            $sql = "SELECT FIPS, Country FROM places_country";
        }else{
            $sql = "SELECT FIPS, Country FROM places_country
                    WHERE Country LIKE '$searchstring%'";
        }
		return $this->db->query($sql);
	}
    
    
}
?>
